//
//  PayAndIncomeView.m
//  Daybook
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "PayAndIncomeView.h"
#import "Common.h"
@interface PayAndIncomeView ()

@property (nonatomic, strong)UILabel *incomeTitleLabel;

@property (nonatomic, strong)UILabel *payTitleLabel;

@end

@implementation PayAndIncomeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}


- (void)createView {
    
    //self.frame = CGRectMake(0, 0, MAIN_WIDTH, (95 / 414.0 * MAIN_WIDTH) / 2);
    self.backgroundColor = [UIColor whiteColor];
    
    self.incomeTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 150 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (20 / 736.0)))];
    self.incomeTitleLabel.backgroundColor = [UIColor whiteColor];
    self.incomeTitleLabel.text = @"收入";
    self.incomeTitleLabel.font = [UIFont systemFontOfSize:15];
    self.incomeTitleLabel.textAlignment = 1;
    [self addSubview:self.incomeTitleLabel];
    
    
    self.incomeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, (MAIN_HEIGHT * (20 / 736.0)), 150 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (25 / 736.0)))];
    self.incomeLabel.backgroundColor = [UIColor clearColor];
    self.incomeLabel.textAlignment = 1;
    [self addSubview:self.incomeLabel];
    
    self.payTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(MAIN_WIDTH - (155 / 414.0 * MAIN_WIDTH), 0, 150 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (20 / 736.0)))];
    self.payTitleLabel.backgroundColor = [UIColor clearColor];
    self.payTitleLabel.text = @"支出";
    self.payTitleLabel.font = [UIFont systemFontOfSize:15];
    self.payTitleLabel.textAlignment = 1;
    [self addSubview:self.payTitleLabel];
    
    
    self.payLabel = [[UILabel alloc]initWithFrame:CGRectMake(MAIN_WIDTH - (155 / 414.0 * MAIN_WIDTH), (MAIN_HEIGHT * (20 / 736.0)), 150 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (25 / 736.0)))];
    self.payLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:self.payLabel];
    self.payLabel.textAlignment = 1;
    
    
//    RLMResults *accountBooks = [Account allObjects];
//    NSNumber *sumMoney = [accountBooks sumOfProperty:@"money"];
//    self.payLabel.text = [NSString stringWithFormat:@"%.2lf", [sumMoney floatValue]];
    self.payLabel.text = [RealmTool payStringFromCurrentAccountFromUserDefaults];
    
//    NSNumber *sumIncome = [accountBooks sumOfProperty:@"income"];
//    
//    self.incomeLabel.text = [NSString stringWithFormat:@"%.2lf", [sumIncome floatValue]];
    self.incomeLabel.text = [RealmTool incomeStringFromCurrentAccountFromUserDefaults];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moneyChange:) name:@"moneyChanged" object:nil];
    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomeChange:) name:@"incomeChanged" object:nil];
    
}

- (void)moneyChange:(NSNotification *)notification {

    
//    RLMResults *accountBooks = [Account allObjects];
//    NSNumber *sumMoney = [accountBooks sumOfProperty:@"money"];
//    self.payLabel.text = [NSString stringWithFormat:@"%.2lf", [sumMoney floatValue]];
    
    self.payLabel.text = [RealmTool payStringFromCurrentAccountFromUserDefaults];

}


- (void)incomeChange:(NSNotification *)notification {
//     RLMResults *accountBooks = [Account allObjects];
//    NSNumber *sumIncome = [accountBooks sumOfProperty:@"income"];
//    self.incomeLabel.text = [NSString stringWithFormat:@"%.2lf", [sumIncome floatValue]];
    self.incomeLabel.text = [RealmTool incomeStringFromCurrentAccountFromUserDefaults];

}



@end
